<?php

namespace App\Api\Logic;

use App\Api\Logic\Validate\BaseValidate;
use App\Api\Logic\Validate\UserValidate;
use App\Exceptions\ValidationException;
use App\Helpers\ApiResponse;
use App\Helpers\Lib\AliSmsServices;
use App\Helpers\ResponseEnum;
use App\Models\TongUser;
use Illuminate\Support\Facades\Cache;

class UserLogic extends BaseValidate
{
    /**
     * @return UserValidate
     */
    private function getValidate():UserValidate{
        return  new UserValidate();
    }

    /**
     * @param $params
     * @return void
     */
    public function user_register($params){
        $validate = $this->getValidate();
        //throw new ValidationException(ResponseEnum::HTTP_ERROR,'系统升级，请先核对自己的数据');
        if(!$validate->scene('user_register')->check($params)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }

        if($params['check_password']!=$params['password']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'两次密码输入不一致');
        }

        $userInfo = TongUser::query()->where('share_code',$params['share_code'])->first();
        if(!$userInfo){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'无效邀请码');
        }
        //判断用户是否发送过短信
        if(!Cache::has(AliSmsServices::$prix.$params['mobile'])){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'验证码错误');
        }

        $cache_code = Cache::get(AliSmsServices::$prix.$params['mobile']);
        if($cache_code!=$params['sms_code']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'验证码错误');
        }
        //短信验证码通过后删除掉缓存
        Cache::forget(AliSmsServices::$prix.$params['mobile']);

    }

    /**
     * @param array $data
     * @return void
     */
    public function userLogin(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('userLogin')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
        //账号登录
        if($data['type']==1){
            if(empty($data['mobile'])){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'请输入账号');
            }
            if(empty($data['password'])){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'请输入密码');
            }
        }else{
            if(empty($data['mobile'])){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'请输入手机号');
            }
            if(empty($data['code'])){
                throw new ValidationException(ResponseEnum::HTTP_ERROR,'请输入验证码');
            }
        }
    }

    /**
     * @param array $data
     * @return void
     * @throws ValidationException
     */
    public function setPayPassword(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('setPayPassword')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
        if($data['check_pay_password']!=$data['pay_password']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'两次密码输入不一致');
        }
    }

    /**
     * 根据手机号获取用户信息
     * @param array $data
     * @return void
     */
    public function getMobileInfo(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getMobileInfo')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
        $query = TongUser::query()->where('mobile',$data['to_mobile']);
        if(!$query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'好友不存在');
        }
    }

    /**
     * 获取用户的分享吗
     * @param array $data
     * @return void
     */
    public function getUserSharePic(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getUserSharePic')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

    /**
     * 获取用户六获取用户流水
     * @param array $data
     * @return void
     */
    public function getUserFlow(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getUserFlow')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

    /**
     * 获取好友的详情
     * @param array $data
     * @return void
     */
    public function getFriendUserInfo(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getFriendUserInfo')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

    /**
     * 重置登录密码
     * @param array $data
     * @return void
     */
    public function resetUserPassword(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('resetUserPassword')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }

        //获取用户信息
        $user_query = TongUser::query()->where('mobile',$data['phone']);
        if(!$user_query->exists()){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'用户信息不存在');
        }
        $user_info = $user_query->first();
        if($user_info['mobile']!=$data['phone']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'注册手机号输入有误');
        }
        //判断用户是否发送过短信
        if(!Cache::has(AliSmsServices::$prix.$data['phone'])){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'验证码错误');
        }
        $cache_code = Cache::get(AliSmsServices::$prix.$data['phone']);
        if($cache_code!=$data['sms_code']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'验证码错误');
        }
        if($user_info['password']==$data['new_password']){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,'新密码不能与旧密码相同');
        }
        //短信验证码通过后删除掉缓存
        Cache::forget(AliSmsServices::$prix.$data['phone']);
    }

    /**
     * 获取我的好友
     * @param array $data
     * @return void
     */
    public function getChildUser(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getChildUser')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

    public function getAllChildUser(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getAllChildUser')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }

    public function getChildUserTwo(array $data)
    {
        $validate = $this->getValidate();
        if(!$validate->scene('getChildUserTwo')->check($data)){
            throw new ValidationException(ResponseEnum::HTTP_ERROR,$validate->getError());
        }
    }
}
